
import 'package:bookshelf/event/icr_bind_event.dart';
import 'package:bookshelf/event/icr_event.dart';
import 'package:bookshelf/util/toast_util.dart';

import '../event/user_id_event.dart';
import '../main.dart';
import '../util/analysis_book_rule.dart';
import '../util/analysis_reader_rule.dart';
import 'three_to_one_manager.dart';

class AnalysisHelper{

  ThreeToOneManager manager = ThreeToOneManager();

  dataReceived(hexString){
    print(hexString);

    //修改eas
    if(manager.mode == Mode.eas){
      manager.setEasSuccess();
      return;
    }

    //修改afi
    if(manager.mode == Mode.afi){
      manager.setAfiSuccess();
      return;
    }
    //密码认证
    if(manager.mode == Mode.auth){
      if(hexString == "040000525A"){
        manager.setAuthSuccess();
      }else{
        manager.setAuthFail();
      }
      return;
    }

    //设置模式
    if(hexString == "040000525A"){
      manager.setModeSuccess();
      return;
    }

    //1443A
    //请求-Request成功
    if(hexString.startsWith("060000")){
      manager.setRequestSuccess();
      return;
    }
    //请求-Request失败
    if(hexString.startsWith("050010")){
      manager.setRequestFailed();
      return;
    }
    //读读者卡成功
    if(hexString.startsWith("080000")){
      if(manager.mode == Mode.reader){
        analyzeId(hexString);
      }
      if(manager.mode == Mode.readerBlock){
        if(hexString.length>14){
          String uid = hexString.substring(6,hexString.length-4);
          manager.setSelect(uid);
        }else{
          toastWarn('读者卡标签异常');
        }
      }
      return;
    }
    //选择
    if(hexString.startsWith("050000")){
      manager.setAuthKey();
      return;
    }
    //读者卡扇区
    if(hexString.startsWith("140000")){
      analyzeId(hexString);
      return;
    }

    //1443B
    //读身份证失败
    if(hexString.startsWith("05001B") || hexString.startsWith('04001f')){
      manager.setRequestFailed();
      return;
    }
    //读身份证成功
    if(hexString.startsWith("0C0000")){
      if(manager.mode == Mode.identify){
        eventFactory.fire(IcrEvent(hexString.substring(6,hexString.length-4)));
        return;
      }
      if(manager.mode == Mode.identifyBind){
        eventFactory.fire(IcrBindEvent(hexString.substring(6,hexString.length-4)));
        return;
      }
      return;
    }



    //14692
    //读标签失败
    if(hexString.startsWith("04000E") || hexString.startsWith("040024")){
      manager.setRequestFailed();
      return;
    }

    //读标签成功
    if(hexString.startsWith("0D0000") && hexString.length>12){
      manager.setInventorySuccess(hexString.substring(8,hexString.length-4));
      return;
    }

    //读标签重复
    if(hexString.startsWith("160000")){
      manager.setRequestFailed();
      return;
    }

    //读块成功
    if(hexString.startsWith("13000000") && hexString.length>36){
      analyzeBlock(hexString);
      return;
    }

    //未知错误
    if(manager.mode == Mode.inventory){
      manager.setRequestFailed();
    }

  }

  analyzeBlock(hexString) {
    int count = 3;
    if (hexString.length >= 6+count*2+count*8+4) {
      var block = '';
      for(int i = 0; i<count;i++){
        int start = 6+(i+1)*2+i*8;
        block = block + hexString.substring(start,start+8);
      }
      var no = analysisBookRule(block);
      manager.setNoSuccess(no);
    }else{
      manager.setNoSuccess(null);
    }
  }

  analyzeId(String str){
    if(str.length>14){
      String uid = str.substring(6,str.length-4);
      var uidNo = analysisReaderRule(uid);
      if(uidNo != null){
        eventFactory.fire(UserIdEvent(uidNo));
      }else{
        manager.setRequestFailed();
        toastWarn('读者卡解析失败');
      }
    }
  }

}